<?php
	/**
	 * Nguoi tao: Phuongtt
	 * ngay tao: 27/12/2010
	 */
// Dinh nghia duong dan den thu vien cua Zend
	set_include_path('../../library/'
			. PATH_SEPARATOR . '../../application/models/'
			. PATH_SEPARATOR . '../../config/');			
	// Goi class Zend_Load
	include "../../library/Zend/Loader.php";		
	Zend_Loader::loadClass('Efy_Init_Config');
	Zend_Loader::loadClass('Zend_Config_Xml');	
	$sConfig = new Efy_Init_Config();
	//Ket noi CSDL SQL theo kieu ADODB
	//Khoi tao session
	session_start();
	//Lay ten ham thuc hien
	$sFunctionName = $_REQUEST['FuntionName'];
	if($sFunctionName != '' && $sFunctionName != null){	
		$sFunctionName .= '();';
		eval($sFunctionName);
	}
	function getList(){
		$arrInput= $_REQUEST;
		$listCode = ($arrInput['listCode'])?$arrInput['listCode']:'';
		$arrResult = getAllObjectbyListCode($listCode);
		echo json_encode($arrResult); 
		exit;
	}
	function getListFull(){
		$arrInput= $_REQUEST;
		$listCode = ($arrInput['listCode'])?$arrInput['listCode']:'';
		$arrResult = getAllObjectbyListCodeFull('',$listCode);
		echo json_encode($arrResult); 
		exit;
	}	
	function lall(){
		$arrInput= $_REQUEST;
		$listCode = ($arrInput['listCode'])?$arrInput['listCode']:'';
		$tagNameList = '';
		$tagValueList = '';		
		$tagNameList .= ($arrInput['province'])?'tinh_thanh;':'';
		$tagValueList .= ($arrInput['province'])?$arrInput['province']. ';':'';
		$tagNameList .= ($arrInput['district'])?'quan_huyen;':'';
		$tagValueList .= ($arrInput['district'])?$arrInput['district']. ';':'';		
		if ($tagNameList != ''){
			$tagNameList = substr($tagNameList, 0, strlen($tagNameList) - 1);
			$tagValueList = substr($tagValueList, 0, strlen($tagValueList) - 1);
		}
		$colNameList = '';
		if ($listCode == 'DM_DON_VI_TN_TKQ')
			$colNameList = 'tinh_thanh;quan_huyen';
		elseif ($listCode == 'DM_DON_VI_THAM_GIA_BHXH')
			$colNameList = 'tinh_thanh;quan_huyen;can_bo_chuyen_quan';
		$arrResult = getListByTagNameList($listCode,$tagNameList, $tagValueList,$colNameList);
		echo json_encode($arrResult); 
		exit;
	}
	//Lay danh sach don vi theo tinh thanh, quan huyen, khoi don vi
	function tr(){
		$arrInput= $_REQUEST;
		$listCode = ($arrInput['listCode'])?$arrInput['listCode']:'';
		$tagNameList = '';
		$tagValueList = '';		
		$tagNameList .= ($arrInput['province'])?'tinh_thanh;':'';
		$tagValueList .= ($arrInput['province'])?$arrInput['province']. ';':'';
		$tagNameList .= ($arrInput['district'])?'quan_huyen;':'';
		$tagValueList .= ($arrInput['district'])?$arrInput['district']. ';':'';		
		if ($tagNameList != ''){
			$tagNameList = substr($tagNameList, 0, strlen($tagNameList) - 1);
			$tagValueList = substr($tagValueList, 0, strlen($tagValueList) - 1);
		}
		$arrResult = getListCodeByTagNameList($listCode,$tagNameList, $tagValueList);
		echo json_encode($arrResult); 
		exit;
	}	
	function bv(){
		$arrInput= $_REQUEST;
		$tagNameList = '';
		$tagValueList = '';		
		$tagNameList .= ($arrInput['province'])?'tinh_thanh;':'';
		$tagValueList .= ($arrInput['province'])?$arrInput['province']. ';':'';		
		if ($tagNameList != ''){
			$tagNameList = substr($tagNameList, 0, strlen($tagNameList) - 1);
			$tagValueList = substr($tagValueList, 0, strlen($tagValueList) - 1);
		}
		$arrResult = getListByTagNameList('DM_DON_VI_Y_TE',$tagNameList, $tagValueList);
		$count = sizeof($arrResult);
		for ($i = 0; $i < $count; $i++)
			$arrResult[$i]['C_NAME'] = $arrResult[$i]['C_CODE'] . ' - ' . $arrResult[$i]['C_NAME'];
		echo json_encode($arrResult); 
		exit;
	}
	function cd(){
		$arrResult = getAllObjectbyListCode('DM_CHE_DO');
		$count = sizeof($arrResult);
		for ($i = 0; $i < $count; $i++)
			$arrResult[$i]['C_NAME'] = $arrResult[$i]['C_CODE'] . ' - ' . $arrResult[$i]['C_NAME'];
		echo json_encode($arrResult); 
		exit;
	}
	function getUnitNameByCode(){
		$arrInput= $_REQUEST;	
		$listCode = ($arrInput['listCode'])?$arrInput['listCode']:'';
		$unitCode = ($arrInput['unitCode'])?$arrInput['unitCode']:'';	
		$tagNameList = '';
		$tagValueList = '';
		$tagNameList .= ($arrInput['province'])?'tinh_thanh;':'';
		$tagValueList .= ($arrInput['province'])?$arrInput['province']. ';':'';
		
		$tagNameList .= ($arrInput['district'])?'quan_huyen;':'';
		$tagValueList .= ($arrInput['district'])?$arrInput['district']. ';':'';
		
		if ($tagNameList != ''){
			$tagNameList = substr($tagNameList, 0, strlen($tagNameList) - 1);
			$tagValueList = substr($tagValueList, 0, strlen($tagValueList) - 1);
		}
		$arrResult = getListByTagNameList($listCode,$tagNameList, $tagValueList);
		foreach ($arrResult as $value){
			if ($unitCode == $value['C_CODE']){
				echo $value['C_NAME'];
				exit;
			}	
		}
	}
	//Lay danh sach tai lieu kem theo
	function id(){
		$arrInput= $_REQUEST;
		$recordType = ($arrInput['recordType'])?$arrInput['recordType']:'';
		$recordTypeName = 'DM_TLKT_' . $recordType;
		$arrResult = getAllObjectbyListCodeFull($_SESSION['OWNER_CODE'], $recordTypeName); 
		echo json_encode($arrResult); 
		exit;
	}
	function getListByTagNameList($listTypeCode,$tagNameList,$tagValueList,$colNameList = ''){
		$objConfig = new Efy_Init_Config();
		$sFilePath = 'http://'.$_SERVER["HTTP_HOST"].$objConfig->_setWebSitePath() . $objConfig->_setXmlFileUrlPath(0) . "list/output/$listTypeCode.xml";
		try {
			$objXmlData = new Zend_Config_Xml($sFilePath,'data_list');
			$arrXmlData = $objXmlData->item->toArray();
			$arrOutput = array();
			$arrTagName = explode(';', $tagNameList);
			$arrTagValue = explode(';', $tagValueList); 
			$len = sizeof($arrTagName);
			if ($colNameList != '')
				$arrayKey = explode(';', $colNameList);
			if (isset($arrXmlData[0]) && is_array($arrXmlData[0])){
				foreach($arrXmlData as $value){					
					if($tagNameList){
						$count = 0;
						for ($i = 0; $i < $len; $i++){
							if ($value[$arrTagName[$i]] != $arrTagValue[$i]){
								$count = 1; break;
							}
						}
						if ($count == 0){
							$name = $value['c_name'];
							if($listTypeCode == 'DM_DON_VI_TN_TKQ')
								$name .= ', ' . $value['dia_chi'];
							$arrTemp = array('C_CODE'=>$value['c_code'],'C_NAME'=>$name);
							if ($colNameList != '')
								foreach ($arrayKey as $value1){
									$key = strtoupper($value1);
									$arrTemp[$key] = $value[$value1];
								}
							array_push($arrOutput,$arrTemp);						
						}
					}else{
						$name = $value['c_name'];
							if($listTypeCode == 'DM_DON_VI_TN_TKQ')
								$name .= ', ' . $value['dia_chi'];
							$arrTemp = array('C_CODE'=>$value['c_code'],'C_NAME'=>$name);
						if ($colNameList != '')
							foreach ($arrayKey as $value1){
								$key = strtoupper($value1);
								$arrTemp[$key] = $value[$value1];
							}
						array_push($arrOutput,$arrTemp);
					}	
				}
			}else{
				if($tagNameList){
					$count = 0;
					for ($i = 0; $i < $len; $i++){
						if ($value[$arrTagName[$i]] != $arrTagValue[$i]){
							$count = 1; break;
						}
					}
					if ($count == 0){
						$name = $value['c_name'];
							if($listTypeCode == 'DM_DON_VI_TN_TKQ')
								$name .= ', ' . $value['dia_chi'];
							$arrTemp = array('C_CODE'=>$value['c_code'],'C_NAME'=>$name);
						if ($colNameList != '')
							foreach ($arrayKey as $value1){
								$key = strtoupper($value1);
								$arrTemp[$key] = $value[$value1];
							}					
						array_push($arrOutput,$arrTemp);
					}
				}else{
					$name = $value['c_name'];
							if($listTypeCode == 'DM_DON_VI_TN_TKQ')
								$name .= ', ' . $value['dia_chi'];
							$arrTemp = array('C_CODE'=>$value['c_code'],'C_NAME'=>$name);
					if ($colNameList != '')
						foreach ($arrayKey as $value1){
							$key = strtoupper($value1);
							$arrTemp[$key] = $value[$value1];
						}					
					array_push($arrOutput,$arrTemp);
				}
			}
			return $arrOutput;
		} catch (Exception $e) {
			return '';
		}
	}
	function getListCodeByTagNameList($listTypeCode,$tagNameList,$tagValueList){
		$objConfig = new Efy_Init_Config();
		$sFilePath = 'http://'.$_SERVER["HTTP_HOST"].$objConfig->_setWebSitePath() . $objConfig->_setXmlFileUrlPath(0) . "list/output/$listTypeCode.xml";
		try {
			$objXmlData = new Zend_Config_Xml($sFilePath,'data_list');
			$arrXmlData = $objXmlData->item->toArray();
			$arrOutput = array();
			$arrTagName = explode(';', $tagNameList);
			$arrTagValue = explode(';', $tagValueList); 
			$len = sizeof($arrTagName);
			if (isset($arrXmlData[0]) && is_array($arrXmlData[0])){
				foreach($arrXmlData as $value){
					if($tagNameList){
						$count = 0;
						for ($i = 0; $i < $len; $i++){
							if ($value[$arrTagName[$i]] != $arrTagValue[$i]){
								$count = 1; break;
							}
						}
						if ($count == 0){
							array_push($arrOutput,array('C_CODE'=>$value['c_code'],'C_NAME'=>$value['c_code']));
						}
					}else{
						array_push($arrOutput,array('C_CODE'=>$value['c_code'],'C_NAME'=>$value['c_code']));
					}	
				}
			}else{
				if($tagNameList){
					$count = 0;
					for ($i = 0; $i < $len; $i++){
						if ($value[$arrTagName[$i]] != $arrTagValue[$i]){
							$count = 1; break;
						}
					}
					if ($count == 0){
						array_push($arrOutput,array('C_CODE'=>$arrXmlData['c_code'],'C_NAME'=>$value['c_code']));
					}
				}else{
					array_push($arrOutput,array('C_CODE'=>$arrXmlData['c_code'],'C_NAME'=>$value['c_code']));
				}
			}
			return $arrOutput;
		} catch (Exception $e) {
			return '';
		}
	}
	/**
	* Nguoi tao: PHUONGTT
	* Ngay tao: 13/10/2011
	* Y nghia:Lay Mang tat ca thong tin danh muc doi tuong cua mot danh muc
	* Input: Ma danh muc
	* Output: Mang cac doi tuong cua loai danh muc ung voi ma truyen vao 
	*/
	function getAllObjectbyListCodeFull($sOwnerCode,$sCode){ 
		$objConfig = new Efy_Init_Config();
		$sFilePath = 'http://'.$_SERVER["HTTP_HOST"].$objConfig->_setWebSitePath() . $objConfig->_setXmlFileUrlPath(0) . "list/output/$sCode.xml";
		try {
			$objXmlData = new Zend_Config_Xml($sFilePath,'data_list');
			$arrXmlData = $objXmlData->item->toArray();
			$arrOutput = array();
			if (isset($arrXmlData[0]) && is_array($arrXmlData[0])){
				$arrayKey = array_keys($arrXmlData[0]);
				foreach($arrXmlData as $value){
					if ($sOwnerCode != ''){
						$arrOwnerCode = explode(',',$value['c_owner_code']);
						if(in_array($sOwnerCode,$arrOwnerCode)){
							$arrTemp = array();
							foreach ($arrayKey as $value1){
								$key = strtoupper($value1);
								$arrTemp[$key] = $value[$value1];
							}
							array_push($arrOutput,$arrTemp);
						}
					}else {
						$arrTemp = array();
						foreach ($arrayKey as $value1){
							$key = strtoupper($value1);
							$arrTemp[$key] = $value[$value1];
						}
						array_push($arrOutput,$arrTemp);
					}
				}
			}else{
				$arrayKey = array_keys($arrXmlData);
				$arrTemp = array();
				foreach ($arrayKey as $value1){
					$key = strtoupper($value1);
					$arrTemp[$key] = $arrXmlData[$value1];
				}
				array_push($arrOutput,$arrTemp);
			}
			return $arrOutput;
		} catch (Exception $e) {
			return '';
		}
	}	
	function getAllObjectbyListCode($sCode){ 
		$objConfig = new Efy_Init_Config();
		$sFilePath = 'http://'.$_SERVER["HTTP_HOST"].$objConfig->_setWebSitePath() . $objConfig->_setXmlFileUrlPath(0) . "list/output/$sCode.xml";
		$objXmlData = new Zend_Config_Xml($sFilePath,'data_list');
		$arrXmlData = $objXmlData->item->toArray();
		$arrOutput = array();
		if (isset($arrXmlData[0]) && is_array($arrXmlData[0])){
			foreach($arrXmlData as $value){
				array_push($arrOutput,array('C_CODE'=>$value['c_code'],'C_NAME'=>$value['c_name']));
			}
		}else{
			array_push($arrOutput,array('C_CODE'=>$arrXmlData['c_code'],'C_NAME'=>$arrXmlData['c_name']));
		}
		return $arrOutput;
	}
	/**
     * @author: Trinh Thanh Phuong
     * Ham phan trang
     * @param unknown_type $iTotalRecord
     * @param unknown_type $iRowOnPage
     */
    function pagingModal(){
    	$arrInput= $_REQUEST;
		$iTotalRecord = ($arrInput['iTotalRecord'])?$arrInput['iTotalRecord']:'';
		$sJsChangePage = (isset($arrInput['JsChangePage']) && $arrInput['JsChangePage'])?$arrInput['JsChangePage']:'gotopageModal';
		$iPage = (isset($arrInput['iPage'])?$arrInput['iPage']:'');
		$iRowOnPage = ($arrInput['iRowOnPage'])?$arrInput['iRowOnPage']:'';
    	$htmlpaging  ='<div id = "paging-content-modal">';
    	$iNumberPage  = ceil($iTotalRecord/$iRowOnPage);     //Tong so trang
		$inumberpageinsheet = 10; 						 	 //so trang hien thi
		$iStartPage	  = 1;								     //Trang dau tien
		$iEndpage	  = 1;								 	 //Trang cuoi cung
		$iaddPage	  = 4;									 //buoc tien cua phan trang
		if($iNumberPage < $inumberpageinsheet){
			$iStartPage   = 1;
			$iEndpage	  = $iNumberPage;
		}else{
			if($iPage > $iaddPage){
				$flag = 0;
				while ($iaddPage >= 0){
					if($iNumberPage - ($iPage - $iaddPage -1) >= $inumberpageinsheet){
						$iStartPage = $iPage - $iaddPage;
						$flag = 1;
						break;
					}
					$iaddPage--;
				}
				if (!$flag)
					$iStartPage   = ($iNumberPage - $inumberpageinsheet) + 1;
			}else{
				$iStartPage = 1;
			}
			$iEndpage = ($iStartPage + $inumberpageinsheet) - 1;
		}
		if($iPage > 1){
			$htmlpaging .='<a class = "pre" onclick ="' . $sJsChangePage .'('.($iPage - 1 ).')">Trước</a>';
		}
		if($iTotalRecord > $iRowOnPage)
			for($i = $iStartPage; $i <= $iEndpage; $i++){
				if($i == $iPage)
					$htmlpaging .='<a class = "pg current" onclick ="' . $sJsChangePage .'('.$i.',this)">'.$i.'</a>';
				else $htmlpaging .='<a class = "pg" onclick ="' . $sJsChangePage .'('.$i.',this)">'.$i.'</a>';
			}
		if($iPage < $iNumberPage){
			$htmlpaging .='<a class = "nex" onclick ="' . $sJsChangePage .'('.($iPage + 1 ).',this)">Tiếp</a>';
		}
    	$htmlpaging  .='</div>';
    	echo $htmlpaging;
    }
	/**
	 * @author: Trinh Thanh Phuong
     * Ham hien thi chon so ban ghi tren trang
     * @param unknown_type $iTotalRecord
     * @param unknown_type $iRowOnPage
	 */
	function _generateChangeRecordNumberPageModal($piValue, $sLabel = ""){		
		$psHtmlString = "";	
		//Chuyen doi thong tin trong xau XML -> Mang
		$arrListItem = Efy_Function_RecordFunctions::getAllObjectbyListCode('',DM_SO_HS_TREN_TRANG);		
		//Tao chuoi HTML
		$psHtmlString = $psHtmlString . "Hiển thị ";
		$psHtmlString = $psHtmlString . "<select class='textbox' id = 'cbo_nuber_record_page_modal' name='cbo_nuber_record_page_modal' optional='true' style='width:60' onChange='changeNumberRowPerPageModal(this)' >";
		$psHtmlString = $psHtmlString . Efy_Library::_generateSelectOption($arrListItem, 'C_CODE', 'C_CODE', 'C_NAME', $piValue);
		$psHtmlString = $psHtmlString . "</select>";
		$strlabel = '';	
		if ($sLabel != ""){
			$strlabel = " $sLabel/1 trang";
		}else{
			$strlabel = " hồ sơ/1 trang";
		}
		$psHtmlString = $psHtmlString . $strlabel;		
		return $psHtmlString;
	}
?>